πŸ”₯ Functions in C Programming

Complete Beginner to Advanced Guide with Deep Explanation, Logic, Line-by-Line Comments & Output

πŸ“˜ Part of the C Programming Beginner Series by Learning Growth Hub
✍️ Written by Krishna Popat


πŸš€ 1️⃣ Introduction to Functions in C

As programs grow larger, writing everything inside main() becomes confusing and difficult to manage.

If you write 500+ lines inside only one function, debugging becomes very hard.

πŸ‘‰ Functions help us:

  • Divide programs into smaller parts

  • Reuse code

  • Improve readability

  • Reduce repetition

  • Write structured programs

Think of a function like:

Input → Processing → Output

🎯 Who Should Read This?

✔ Complete beginners
✔ BCA / B.Tech / Diploma students
✔ Students preparing for exams
✔ Anyone learning C fundamentals


πŸ“Œ 2️⃣ What is a Function?

A function is a block of code that performs a specific task.

Instead of writing the same logic again and again, we define it once and call it whenever needed.


3️⃣ Structure of a Function

1️⃣ Function Declaration

return_type function_name(parameters);

2️⃣ Function Definition

return_type function_name(parameters) { // body }

3️⃣ Function Call

function_name(arguments);

🧠 4️⃣ Basic Example

No Arguments, No Return Value

πŸ”Ž Understanding 

  • Function does not take any input

  • Function does not return anything

  • It only performs a task (printing message)

  • Control goes: main()greet() → back to main()


#include <stdio.h> // Include standard input-output library void greet() // Define function greet with no return value { printf("Welcome to Learning Growth Hub!\n"); // Print message on screen } int main() // Main function - execution starts here { greet(); // Call greet function return 0; // End program successfully }

πŸ–₯ Output

Welcome to Learning Growth Hub!

🧩 5️⃣ Types of Functions in C

1️⃣ No arguments, No return value
2️⃣ With arguments, No return value
3️⃣ No arguments, With return value
4️⃣ With arguments, With return value


πŸ”Ή Type 1: No Arguments, No Return Value

πŸ”Ž Understanding 

  • Function performs a task

  • No input is given

  • Nothing is returned

  • Used for display or printing tasks


#include <stdio.h> // Include input-output library void display() // Define function without parameters { printf("Hello Krishna!\n"); // Print greeting message } int main() // Main function { display(); // Call display function return 0; // End program }

πŸ–₯ Output

Hello Krishna!

πŸ”Ή Type 2: With Arguments, No Return Value

πŸ”Ž Understanding 

  • Function receives values from main

  • Performs calculation

  • Prints result inside function

  • Does not return anything

Flow:
main → add(5,3) → prints result → back to main


#include <stdio.h> // Include input-output library void add(int a, int b) // Function with two parameters { int sum = a + b; // Add a and b printf("Sum = %d\n", sum); // Print sum } int main() // Main function { add(5, 3); // Pass 5 and 3 to function return 0; // End program }

πŸ–₯ Output

Sum = 8

πŸ”Ή Type 3: No Arguments, With Return Value

πŸ”Ž Understanding

  • Function takes input

  • Returns value

  • main() stores returned value

  • Returned value is used later

Flow:
main → getNumber → return value → main prints it


#include <stdio.h> // Include input-output library int getNumber() // Function returning integer { int num; // Declare integer variable printf("Enter number: "); // Ask user to enter number scanf("%d", &num); // Store user input in num return num; // Return num to main } int main() // Main function { int n = getNumber(); // Call function and store returned value printf("You entered: %d", n); // Print value return 0; // End program }

πŸ–₯ Output

Enter number: 7 You entered: 7

πŸ”Ή Type 4: With Arguments, With Return Value

πŸ”Ž Understanding 

  • Most commonly used type

  • Function receives input

  • Performs calculation

  • Returns result

  • main() prints result


#include <stdio.h> // Include input-output library int multiply(int x, int y) // Function with parameters and return type { int result = x * y; // Multiply x and y return result; // Return result } int main() // Main function { int output = multiply(4, 5); // Call function and store result printf("Result = %d", output); // Print result return 0; // End program }

πŸ–₯ Output

Result = 20


After understanding normal functions, let’s explore a powerful concept called Recursion.



πŸ” Recursion in C

Recursion is when a function calls itself to solve a smaller part of a problem.

Every recursive function must have:

✔ Base case → stops recursion
✔ Recursive case → calls function again


πŸ” Example: Factorial Using Recursion

int factorial(int n) // Function definition: takes integer n and returns factorial { if(n == 0 || n == 1) // Base case: if n is 0 or 1 return 1; // Stop recursion and return 1 (because 0! = 1 and 1! = 1) else // Recursive case: if n is greater than 1 return n * factorial(n - 1); // Multiply n with factorial of (n - 1) // Function calls itself with smaller value }


🧠 How It Works (For 5)

factorial(5)
→ 5 × factorial(4)
→ 5 × 4 × factorial(3)
→ 5 × 4 × 3 × factorial(2)
→ 5 × 4 × 3 × 2 × factorial(1)
→ 120


⚠ Important

If base case is missing → infinite recursion → stack overflow.


πŸ›  6️⃣ Practice Programs


✅ Program 1: Square of a Number

πŸ”Ž Understanding /

  • Function receives a number

  • Multiplies number by itself

  • Returns square

  • main() prints result


#include <stdio.h> // Include input-output library int square(int n) // Function to calculate square { return n * n; // Return n multiplied by n } int main() // Main function { int num; // Declare variable printf("Enter number: "); // Ask user for input scanf("%d", &num); // Read input printf("Square = %d", square(num)); // Call function and print result return 0; // End program }

πŸ–₯ Output

Enter number: 6 Square = 36

✅ Program 2: Even or Odd

πŸ”Ž Understanding 

  • Check if number divisible by 2

  • If remainder is 0 → Even

  • Else → Odd


#include <stdio.h> // Include input-output library void checkEvenOdd(int n) // Function to check even or odd { if(n % 2 == 0) // Check divisibility by 2 printf("Even Number"); // Print Even if condition true else // Otherwise printf("Odd Number"); // Print Odd } int main() // Main function { int num; // Declare variable printf("Enter number: "); // Ask input scanf("%d", &num); // Read input checkEvenOdd(num); // Call function return 0; // End program }

πŸ–₯ Output

Enter number: 9 Odd Number

✅ Program 3: Factorial Using Function

πŸ”Ž Understanding 

  • Initialize factorial = 1

  • Multiply numbers from 1 to n

  • Return result


#include <stdio.h> // Include input-output library int factorial(int n) // Function to calculate factorial { int i; // Declare loop variable int fact = 1; // Initialize factorial to 1 for(i = 1; i <= n; i++) // Loop from 1 to n { fact = fact * i; // Multiply fact by i } return fact; // Return factorial result } int main() // Main function { int num; // Declare variable printf("Enter number: "); // Ask input scanf("%d", &num); // Read input printf("Factorial = %d", factorial(num)); // Call function and print result return 0; // End program }

πŸ–₯ Output

Enter number: 5 Factorial = 120

πŸ”₯ 7️⃣ Advanced Program 1: Prime Number (Optimized)

πŸ”Ž Understanding 

  • Numbers ≤ 1 are not prime

  • Check divisibility only up to √n

  • If divisible → Not prime

  • If no divisor → Prime


#include <stdio.h> // Include input-output library int isPrime(int n) // Function to check prime { int i; // Declare loop variable if(n <= 1) // Check invalid case return 0; // Not prime for(i = 2; i * i <= n; i++) // Loop up to square root of n { if(n % i == 0) // If divisible return 0; // Not prime } return 1; // Prime number } int main() // Main function { int num; // Declare variable printf("Enter number: "); // Ask input scanf("%d", &num); // Read input if(isPrime(num)) // Call function printf("Prime Number"); else printf("Not Prime Number"); return 0; // End program }

πŸ–₯ Output

Enter number: 7 Prime Number

πŸ”₯ 8️⃣ Advanced Program 2: Armstrong Number

πŸ”Ž Understanding 

  • Store original number

  • Separate digits

  • Cube each digit

  • Add cubes

  • Compare with original


#include <stdio.h> // Include input-output library int isArmstrong(int n) // Function to check Armstrong number { int original = n; // Store original value int remainder; // Store digit int sum = 0; // Initialize sum while(n > 0) // Loop until number becomes 0 { remainder = n % 10; // Get last digit sum += remainder * remainder * remainder; // Add cube of digit n = n / 10; // Remove last digit } if(sum == original) // Compare with original return 1; // Armstrong number else return 0; // Not Armstrong } int main() // Main function { int num; // Declare variable printf("Enter number: "); // Ask input scanf("%d", &num); // Read input if(isArmstrong(num)) // Call function printf("Armstrong Number"); else printf("Not Armstrong Number"); return 0; // End program }

πŸ–₯ Output

Enter number: 153 Armstrong Number




πŸ”₯ 9️⃣ Advanced Program: Fibonacci Using Recursion

πŸ”Ž Understanding

Fibonacci series:
0 1 1 2 3 5 8 13 21 ...

✔ Formula:
F(n) = F(n-1) + F(n-2)

✔ Base cases:
F(0) = 0
F(1) = 1

✔ Function calls itself twice

⚠ Note: This recursive Fibonacci method is not efficient for large values because it has exponential time complexity O(2ⁿ).

#include <stdio.h> // Include standard input-output library int fibonacci(int n) // Recursive function to find nth Fibonacci number { if(n == 0) // Base case 1 return 0; // Return 0 if n is 0 else if(n == 1) // Base case 2 return 1; // Return 1 if n is 1 else // Recursive case return fibonacci(n - 1) + fibonacci(n - 2); // Function calls itself twice } int main() // Main function - execution starts here { int num; // Declare variable printf("Enter position: "); // Ask user for input scanf("%d", &num); // Read input printf("Fibonacci number = %d", fibonacci(num)); // Call recursive function return 0; // End program successfully }

πŸ–₯ Output

Enter position: 6 Fibonacci number = 8



πŸ“Š Visual Comparison: Function Declaration vs Definition

Now that we understand both concepts separately, let’s look at a quick side-by-side comparison to clearly see the differences.

The table below summarizes the purpose, structure, memory behavior, and usage of both function declaration and function definition in C programming.





πŸ“ Understanding the Comparison

From the comparison above, we can clearly see:

  • A function declaration only informs the compiler that the function exists.

  • A function definition provides the complete implementation of that function.

  • Declarations end with a semicolon (;), while definitions use curly braces { }.

  • Only the function definition contains executable code.

In simple words:

Declaration = What the function looks like
Definition = How the function works

This difference becomes especially important when working with header files and large programs.




“Why Functions Are Important in Real Life?”

Add this small section before conclusion:

🌍 Real-Life Use of Functions

Functions are used in:

  • Banking systems (calculate interest)

  • E-commerce apps (calculate total price)

  • Games (score calculation)

  • Calculator apps

  • Login systems

  • Large software projects

This shows practical relevance (very important for students).


🎯 Final Conclusion

Now you have learned:

✔ Structure of function
✔ All 4 types
✔ Practice programs
✔ Advanced programs
✔ Optimized logic
✔ Full line-by-line comments
✔ Understanding before every program

Functions are the backbone of structured programming in C.



πŸ“Œ Keep Learning. Keep Coding. Keep Growing.

✨ Written by Krishna Popat
🌱 Founder, Learning Growth Hub

Comments

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”